# 导人Web测试驱动库
from selenium import webdriver
# 导人交互操作ActionChains(鼠标指针悬停、滚动、拖放等操作)
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.common import exceptions as ex
from time import sleep

try:
    # 选择需要测试的浏览器
    # driver: WebDriver = webdriver.Firefox(executable_path='geckodriver')
    # 等待加载超时时间
    driver: WebDriver = webdriver.Chrome(executable_path='chromedriver')
    driver.implicitly_wait(30)
    # 打开博学谷主页
    driver.get("https://www.boxuegu.com/")
    # 获取登录按钮ID进行单机操作
    # driver.find_element(By.XPATH, "/html/body/div[1]/div/div/header/nav/div[2]/div[3]/div[1]/span[1]").click()
    driver.find_element(By.CLASS_NAME, "login-btn").click()
    # 再次点击登录按钮
    driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/p[1]/span").click()
    # 选定账号输入框
    driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[1]/div/div/input")
    # 输入账号
    driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[1]/div/div/input").send_keys(
        '18907594441')
    # 选定密码输入框
    driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[2]/div/div[1]/input")
    # 输入密码
    driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[2]/div/div[1]/input").send_keys(
        'clgr2547.')
    # 单机【登录】按钮
    driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/div[2]/button").click()
    # 定义需要查找的属性信息【个人中心】
    sleep(1)
    driver.find_element(By.CLASS_NAME, "photo").click()
    mouse = driver.find_element(By.CLASS_NAME, "photo")
    # 鼠标指针悬停操作，鼠标指针指向个人中心菜单后会显示下拉菜单
    ActionChains(driver).move_to_element(mouse).perform()
    # 在【个人中心】的下拉菜单中单击【退出选项】
    driver.find_element(By.CSS_SELECTOR, "span.sign-out-btn").click()

except ex.StaleElementReferenceException:
        # 睡眠3秒，等待页面加载完成
        sleep(2)
        # 定义需要查找的属性信息【个人中心】
        driver.find_element(By.CLASS_NAME, "photo").click()
        mouse = driver.find_element(By.CLASS_NAME, "photo")
        # 鼠标指针悬停操作，鼠标指针指向个人中心菜单后会显示下拉菜单
        ActionChains(driver).move_to_element(mouse).perform()
        # 在【个人中心】的下拉菜单中单击【退出选项】
        # 睡眠3秒，等待页面加载完成
        sleep(2)
        driver.find_element(By.CLASS_NAME, "sign-out-btn").click()
